package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.RoleMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(interfaceClass = RoleService.class)
public class RoleServiceImpl implements RoleService{

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> findAll() {
        return roleMapper.findAll();
    }

    @Override
    public List<Integer> findRoleIdsByUserId(Integer id) {
        return roleMapper.findRoleIdsByUserId(id);
    }
    /**
     * 检查组编辑更新
     * @param permissionIds
     * @param menuIds
     * @param role
     */
    @Override
    public void edit(Integer[] permissionIds, Integer[] menuIds, Role role) {
        //通过检查组的id删除检查项
        roleMapper.deleteAssociation1(role.getId());
        roleMapper.deleteAssociation2(role.getId());
        //重新插入中间表数据
        setRoleAndPermission(permissionIds,role.getId());
        setRoleAndMenu(menuIds,role.getId());
        //更新检查组数据
        roleMapper.edit(role);
    }

    @Override
    public void add(Integer[] permissionIds, Integer[] menuIds, Role role) {

        //新增数据插入到t_role
        roleMapper.add(role);

        //新增角色和菜单的中间表数据
        setRoleAndPermission(permissionIds,role.getId());
        setRoleAndMenu(menuIds,role.getId());
    }

    private void setRoleAndPermission(Integer[] permissionIds, Integer id) {
        if (permissionIds!=null && permissionIds.length>0){
            //将添加角色和权限的中间表数据
            roleMapper.setRoleAndPermission(permissionIds,id);
        }
    }
    private void setRoleAndMenu(Integer[] menuIds, Integer id) {
        if (menuIds!=null && menuIds.length>0){
            //将添加角色和菜单的中间表数据
            roleMapper.setRoleAndMenu(menuIds,id);
        }
    }



    /*private void setCheckGroupAndCheckItem(Integer[] checkItemIds, Integer id) {

        if (checkItemIds!=null && checkItemIds.length>0){

            //将添加检查组和检查项的中间表数据
            for (Integer checkItemId : checkItemIds) {
                checkGroupMapper.setCheckGroupAndCheckItem(checkItemId,id);
            }

        }


    }*/

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        //定义页面大小和当前页的页码
        PageHelper.startPage(currentPage,pageSize);
        //条件查询检查组信息
        List<Role> list = roleMapper.findByCondition(queryString);
        //包装数据
        PageInfo<Role> pageInfo = new PageInfo<>(list);

        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 通过id查询检查组
     * @param id
     * @return
     */
    @Override
    public Role findById(Integer id) {
        return roleMapper.findById(id);
    }




    /**
     * 通过id删除检查组
     * @param id
     */
    @Override
    public void deleteById(Integer id) {

        //查询当前检查组是否被套餐引用
        Integer count = roleMapper.findCountById(id);
        if (count>0){
            //抛出异常，提示错误信息
            throw new RuntimeException(MessageConstant.CHECKGROUP_IS_QUOTED);
        }

        //删除检查组和检查项中间表数据
        roleMapper.deleteAssociation1(id);
        roleMapper.deleteAssociation2(id);

        //删除检查组
        roleMapper.deleteById(id);

    }

    /**
     * 查询所有权限和菜单信息
     * @return
     */
    @Override
    public List<Permission> findPermission() {
        return roleMapper.findPermission();
    }
    @Override
    public List<Menu> findMenu() {
        return roleMapper.findMenu();
    }
    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        return roleMapper.findPermissionIdsByRoleId(id);
    }
    @Override
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        return roleMapper.findMenuIdsByRoleId(id);
    }
}
